// 导入promise请求
import http from '../../utils/http_promise.js';
import Notify from '@vant/weapp/notify/notify';

// pages/my_houses/my_houses.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
     houseList:[]
  },

  sale(e){
    
     
     let flag = e.currentTarget.dataset.flag;
     let id = e.currentTarget.dataset.id;
    let index = e.currentTarget.dataset.index;

     let state = flag == 'up' ? 1:0;

     http.request({

         method:'POST',
         requireAuth:true,
         url:'house/state',
         data:{
           id,state
         }

     })

     .then(res=>{
        console.log(res);
        //this.getHouseList();
        
        let item = this.data.houseList[index];
        console.log(item);

        this.setData({
           ["item.state"]: state == 1 ? true:false
        })
        
     })
     

  },

  edit(e){
     let id = e.currentTarget.dataset.id;
     wx.navigateTo({
        url: '/pages/edit_house/edit_house?id='+id
     })
  },

  del(e){

    console.log(e);
    let id = e.currentTarget.dataset.id;
    let that = this;

    wx.showModal({
      title: '提示',
      content: '您确认要删除房源吗?',
      success(res) {

        if (res.confirm) {

            http.request({
                requireAuth:true,
               'method':"post",
               url:'house/del',
               data:{id}
            })
            .then(res=>{
               console.log(res);
              Notify({ type: 'success', message: '删除成功!' });
               that.getHouseList();
            })

        }else{
           Notify({ type: 'primary', message: '已取消删除!' });
        } 
        
      }
    })

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    this.getHouseList();
    
  },

  getHouseList(){

    http.request({
      url: 'user/my_houses',
      requireAuth: true
    })

    .then(res => {
      console.log(res);
      this.setData({
        houseList: res.data.data
      })
    })
    
  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})